package com.ali.sso.common.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;

import com.ali.sso.common.model.AccessTokenEntity;
import com.ali.sso.common.model.AuthorizationCodeEntity;
import com.ali.sso.common.model.OauthClient;
import com.ali.sso.common.model.UserLogin;
import com.ali.sso.common.service.IOAuthService;

public class DefaultOAuthServiceImpl implements IOAuthService {
	
	private JdbcTemplate jdbcTemplate;
	
	public DefaultOAuthServiceImpl(){
		
	}
	
	public DefaultOAuthServiceImpl(JdbcTemplate jdbcTemplate){
		this.jdbcTemplate = jdbcTemplate;
	}
	
	@Override
	public void saveAuthorizationCode(String userLoginId, String authorizationCode) {
		String querySql = "select * from oauthorization_code where user_login_id='" + userLoginId + "'";
		AuthorizationCodeEntity authCode = jdbcTemplate.query(querySql, new ResultSetExtractor<AuthorizationCodeEntity>(){
			
			@Override
			public AuthorizationCodeEntity extractData(ResultSet result) throws SQLException, DataAccessException {
				if(result.next()){
					AuthorizationCodeEntity ac = new AuthorizationCodeEntity();
					ac.setUserLoginId(result.getString("user_login_id"));
					ac.setAuthorizationCode(result.getString("authorization_code"));
					ac.setGenDate(result.getDate("gen_date"));
					return ac;
				}
				return null;
			}
			
		});
		
		if(authCode == null){
			authCode = new AuthorizationCodeEntity();
			authCode.setUserLoginId(userLoginId);
			authCode.setAuthorizationCode(authorizationCode);
			authCode.setGenDate(new Date());
			String insertSql = "insert into oauthorization_code values(?, ?, ?)";
			jdbcTemplate.update(insertSql, new Object[]{userLoginId, authorizationCode, new Date()});
		}else{
			authCode.setAuthorizationCode(authorizationCode);
			authCode.setGenDate(new Date());
			String updateSql = "update oauthorization_code set authorization_code=?, gen_date=? where user_login_id=?";
			jdbcTemplate.update(updateSql, new Object[]{authorizationCode, authCode.getGenDate(), userLoginId});
		}
	}

	@Override
	public void saveAccessToken(String userLoginId, String accessToken) {
		String querySql = "select * from access_token where user_login_id='" + userLoginId + "'";
		AccessTokenEntity accToken = jdbcTemplate.query(querySql, new ResultSetExtractor<AccessTokenEntity>(){
			
			@Override
			public AccessTokenEntity extractData(ResultSet result) throws SQLException, DataAccessException {
				if(result.next()){
					AccessTokenEntity at = new AccessTokenEntity();
					at.setUserLoginId(result.getString("user_login_id"));
					at.setAccessToken(result.getString("access_token"));
					at.setGenDate(result.getDate("gen_date"));
					return at;
				}
				return null;
			}
			
		});

		if(accToken == null){
			accToken = new AccessTokenEntity();
			accToken.setUserLoginId(userLoginId);
			accToken.setAccessToken(accessToken);
			accToken.setGenDate(new Date());
			String insertSql = "insert into access_token values(?, ?, ?)";
			jdbcTemplate.update(insertSql, new Object[]{userLoginId, accessToken, new Date()});
		}else{
			accToken.setAccessToken(accessToken);
			accToken.setGenDate(new Date());
			String updateSql = "update access_token set access_token=?, gen_date=? where user_login_id=?";
			jdbcTemplate.update(updateSql, new Object[]{accessToken, accToken.getGenDate(), userLoginId});
		}
	}
	
	@Override
	public OauthClient findOauthClient(String clientId) {
		String querySql = "select * from oauth_client where client_id='" + clientId + "'";
		OauthClient oc = jdbcTemplate.query(querySql, new ResultSetExtractor<OauthClient>(){
			
			@Override
			public OauthClient extractData(ResultSet result) throws SQLException, DataAccessException {
				if(result.next()){
					OauthClient ac = new OauthClient();
					ac.setClientId(result.getString("client_id"));
					ac.setClientName(result.getString("client_name"));
					ac.setClientSecret(result.getString("client_secret"));
					return ac;
				}
				return null;
			}
			
		});
		return oc;
	}

	@Override
	public OauthClient findOauthClient(String clientId, String clientSecret) {
		String querySql = "select * from oauth_client where client_id='" + clientId + "' and client_secret='" + clientSecret + "'";
		OauthClient oc = jdbcTemplate.query(querySql, new ResultSetExtractor<OauthClient>(){
			
			@Override
			public OauthClient extractData(ResultSet result) throws SQLException, DataAccessException {
				if(result.next()){
					OauthClient ac = new OauthClient();
					ac.setClientId(result.getString("client_id"));
					ac.setClientName(result.getString("client_name"));
					ac.setClientSecret(result.getString("client_secret"));
					return ac;
				}
				return null;
			}
			
		});
		return oc;
	}
	
	@Override
	public AuthorizationCodeEntity findAuthorizationCode(String authorizationCode) {
		String querySql = "select * from oauthorization_code where authorization_code='" + authorizationCode + "'";
		AuthorizationCodeEntity oc = jdbcTemplate.query(querySql, new ResultSetExtractor<AuthorizationCodeEntity>(){
			
			@Override
			public AuthorizationCodeEntity extractData(ResultSet result) throws SQLException, DataAccessException {
				if(result.next()){
					AuthorizationCodeEntity ac = new AuthorizationCodeEntity();
					ac.setUserLoginId(result.getString("user_login_id"));
					ac.setAuthorizationCode(result.getString("authorization_code"));
					ac.setGenDate(result.getDate("gen_date"));
					return ac;
				}
				return null;
			}
			
		});
		return oc;
	}

	@Override
	public AccessTokenEntity findAccessToken(String accessToken) {
		String querySql = "select * from access_token where access_token='" + accessToken + "'";
		AccessTokenEntity oc = jdbcTemplate.query(querySql, new ResultSetExtractor<AccessTokenEntity>(){
			
			@Override
			public AccessTokenEntity extractData(ResultSet result) throws SQLException, DataAccessException {
				if(result.next()){
					AccessTokenEntity ac = new AccessTokenEntity();
					ac.setUserLoginId(result.getString("user_login_id"));
					ac.setAccessToken(result.getString("access_token"));
					ac.setGenDate(result.getDate("gen_date"));
					return ac;
				}
				return null;
			}
			
		});
		return oc;
	}

	@Override
	public UserLogin findUserLogin(String userLoginId) {
		String querySql = "select * from user_login where user_login_id='" + userLoginId + "'";
		UserLogin userLogin = jdbcTemplate.query(querySql, new ResultSetExtractor<UserLogin>(){
			
			@Override
			public UserLogin extractData(ResultSet result) throws SQLException, DataAccessException {
				if(result.next()){
					UserLogin ac = new UserLogin();
					ac.setUserLoginId(result.getString("user_login_id"));
					ac.setPassword(result.getString("password"));
					return ac;
				}
				return null;
			}
			
		});
		return userLogin;
	}

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
}
